#include "mixerwidget.h"
#include "globalconfig.h"
#include "dm/dmdef.h"


MixerWidget::MixerWidget(QWidget *parent)
    : AGroupBox{tr(LABEL_MIXER), parent}
{
    auto layout = new QVBoxLayout(this);
    auto gLayout = new QGridLayout;

    layout->addStretch();
    layout->addLayout(gLayout);
    layout->addStretch();

    const auto space = 20 * ratio();
    layout->setContentsMargins(space, space, space, space);
    gLayout->setVerticalSpacing(30 * ratio());
    gLayout->setHorizontalSpacing(10 * ratio());

    const auto cfgName = GlobalConfig::getInstance().cfgName();
    const auto cfgDev = GlobalConfig::getInstance().cfgDev();

    for (int i = 0; i < cfgDev.mus.tru_ch + 1; ++i) {
        QVector<MixerButton *> vect;
        for (int j = 0; j < cfgDev.out.tru_ch + 1; ++j) {
            if (0 == i && 0 == j) {

            }
            else if (0 == j) {
                auto label = new ALabel(cfgName.mus[i - 1], this);
                gLayout->addWidget(label, i, j, Qt::AlignVCenter | Qt::AlignRight);
                m_inputs.append(label);
            }
            else if (0 == i) {
                auto label = new ALabel(cfgName.out[j - 1], this);
                gLayout->addWidget(label, i, j, Qt::AlignHCenter | Qt::AlignBottom);
                m_outputs.append(label);
            }
            else {
                auto button = new MixerButton(j - 1 + OUT_CH1, i - 1 + MUS_CH1, this);
                connect(button, &MixerButton::stateChanged, this, &MixerWidget::stateChanged);
                gLayout->addWidget(button, i, j, Qt::AlignHCenter);
                vect.append(button);
            }
        }
        if (i != 0) {
            m_mixers.append(vect);
        }
    }

    connect(this, &MixerWidget::writeMixer, &GlobalConfig::getInstance(), &GlobalConfig::mixer);

    connect(&GlobalConfig::getInstance(), &GlobalConfig::refresh, this, &MixerWidget::refresh);
    connect(&GlobalConfig::getInstance(), &GlobalConfig::inputNameUpdated, this, &MixerWidget::inputNameUpdated);
    connect(&GlobalConfig::getInstance(), &GlobalConfig::outputNameUpdated, this, &MixerWidget::outputNameUpdated);
}

void MixerWidget::refresh()
{
    const auto cfgDev = GlobalConfig::getInstance().cfgDev();
    const auto group = GlobalConfig::getInstance().paramGroup();
    for (int i = 0; i < cfgDev.mus.tru_ch; ++i) {
        for (int j = 0; j < cfgDev.out.tru_ch; ++j) {
            m_mixers[i][j]->set(group.OutCh[j].mixer[i].vol);
        }
    }

}

void MixerWidget::inputNameUpdated()
{
    const auto cfgName = GlobalConfig::getInstance().cfgName();
    for (int i = 0; i < m_inputs.size(); ++i) {
        m_inputs.at(i)->setText(cfgName.mus[i]);
    }
}

void MixerWidget::outputNameUpdated()
{
    const auto cfgName = GlobalConfig::getInstance().cfgName();
    for (int i = 0; i < m_outputs.size(); ++i) {
        m_outputs.at(i)->setText(cfgName.out[i]);
    }
}

void MixerWidget::stateChanged(uint8_t out, uint8_t mus, uint8_t vol)
{
    stDataMixer data;
    data.vol = vol;
    emit writeMixer(out, mus, data);
}
